//
//  UIView+CircleBg.m
//  JuanRoobo
//
//  Created by Zhi Kuiyu on 15/2/26.
//  Copyright (c) 2015年 Zhi Kuiyu. All rights reserved.
//

#import "UITextField+CircleBg.h"
#import <objc/runtime.h>

#define SX(value) value
#define mImageByName(name) [UIImage imageNamed:(name) ]

@implementation UITextField (CircleBg)

@dynamic offset;



#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"

- (void)circlebackGround{
   
//    self.offset = SX(40);
//    self.textAlignment = NSTextAlignmentCenter;
    
    
//    self.font = [self GetFontSize:1];
    self.backgroundColor = [UIColor clearColor] ;
    self.clearButtonMode = UITextFieldViewModeWhileEditing ;
    
    
    UIView * view = [[UIView alloc] initWithFrame:self.bounds];
    view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
    UIViewAutoresizingFlexibleWidth |
    UIViewAutoresizingFlexibleHeight ;
    [self insertSubview:view atIndex:0];
    view.userInteractionEnabled = NO;
    view.clipsToBounds = YES;
    view.layer.frame = self.bounds;
    view.height -= 2;

    view.layer.borderColor = [UIColor colorWithRed:0.525 green:0.584 blue:0.643 alpha:1.000].CGColor;
    view.layer.cornerRadius = SX(45)/2.f;
    view.layer.borderWidth = 1;
    [view.layer setShadowOffset:CGSizeMake(0, 0)];
    [view.layer setShadowColor:[[UIColor clearColor] CGColor]];
    [view.layer setShadowOpacity:1];
    view.layer.shadowRadius = 1.0;//半径

//    [self setBgView:view];
//    
//    self.textColor = mRGBToColor(0x505a66);
    
}

- (void)loadPublicStyle{
    self.offset = SX(10);
//    self.font = [self GetFontSize:1];
    self.backgroundColor = [UIColor clearColor] ;
    self.clearButtonMode = UITextFieldViewModeWhileEditing ;
//    self.textColor = mRGBToColor(0x505a66);

}


- (void)hiddenWarm{
    if(self.warmView){
        self.rightView.hidden = NO;

        [self.warmView removeFromSuperview] ;
//        self.bgView.layer.borderColor = [UIColor colorWithRed:0.525 green:0.584 blue:0.643 alpha:1.000].CGColor;

        self.warmView = nil;
    }
}

- (void)warmViewWithTitle:(NSString *)title{
    
    [self hiddenWarm];
    
    UILabel * warmInfo = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, SX(16))];
    warmInfo.text = title;
    warmInfo.backgroundColor = [UIColor clearColor];
    warmInfo.textColor = [UIColor whiteColor];
    warmInfo.textAlignment = 1;
//    warmInfo.font = [self GetFontSize:-2];
    [warmInfo sizeToFit];

    float marge = SX(14);
    float width = warmInfo.width + marge * 2;
    
    
    UIView * view = [[UIView alloc]  initWithFrame:CGRectMake(self.right  - width, self.top  + SX(15), width, SX(55))] ;
    UIImageView * icon = [[UIImageView alloc] initWithFrame:CGRectMake(view.width - 40, 0, SX(15), SX(15))];
    icon.image = mImageByName(@"icon_error.png");
    [view addSubview:icon];
    
    UIImageView * infoImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, icon.bottom + SX(2), width, SX(46.5))];
    infoImage.backgroundColor = [UIColor clearColor] ;
    infoImage.image = [mImageByName(@"bg_login_tips") resizableImageWithCapInsets:UIEdgeInsetsMake(SX(17), SX(17), SX(23), SX(52))];
    [view addSubview:infoImage] ;

    warmInfo.frame = CGRectMake(0, infoImage.bottom - SX(30), width, SX(14));
    [view addSubview:warmInfo] ;
    
    [self.superview addSubview:view];
    view.userInteractionEnabled = NO;
    
    self.warmView = view;


}

- (void)showWarmWithTitle:(NSString *)title{
    
    if(self.isFirstResponder)
        [self resignFirstResponder];
//    self.bgView.layer.borderColor = [UIColor redColor].CGColor;
    [self warmViewWithTitle:title];
    self.rightView.hidden = YES;

}


- (void)setPlaceholderString:(NSString *)placeholder{

    NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:placeholder];
    [title addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xb5b8bc) range:NSMakeRange(0, placeholder.length)];
    ;

    self.attributedPlaceholder = title;
}

- (void)setLeftImageName:(NSString *)imageNamed{
    self.leftViewMode = UITextFieldViewModeAlways;
    
    UIButton * imageView = [[UIButton alloc] initWithFrame:CGRectMake(5, -2, self.height  - 10, self.height - 2)];
    [imageView setImage:mImageByName(imageNamed) forState:UIControlStateNormal];
    self.leftView = imageView;

}

- (void)setOffset:(float)offset{
    
    objc_setAssociatedObject(self, @"offsetkey", [NSNumber numberWithFloat:offset], OBJC_ASSOCIATION_RETAIN);

}

- (float)offset{
    NSNumber * num = objc_getAssociatedObject(self, @"offsetkey");
    return [num floatValue];

}

//控制 placeHolder 的位置，左右缩 20
- (CGRect)textRectForBounds:(CGRect)bounds {
    bounds.origin.x = self.offset;
    bounds.size.width -= self.offset;
    return bounds;
   
}

// 控制文本的位置，左右缩 20
- (CGRect)editingRectForBounds:(CGRect)bounds {
    UIView * rightView = self.rightView;
    if(rightView){
        return CGRectMake(self.offset, bounds.origin.y, bounds.size.width -  rightView.width -  self.offset, CGRectGetHeight(bounds));
    }

    return CGRectInset( bounds , self.offset , 0 );
}

- (CGRect)clearButtonRectForBounds:(CGRect)bounds
{
    return CGRectMake(bounds.size.width - 50, bounds.origin.y, 50, bounds.size.height);
}

- (CGRect)rightViewRectForBounds:(CGRect)bounds{

    return CGRectMake(bounds.size.width - bounds.size.height, bounds.origin.y, bounds.size.height, bounds.size.height);
}

- (CGRect)leftViewRectForBounds:(CGRect)bounds{

    return CGRectMake(10, -1, bounds.size.height - 15, bounds.size.height);
}


- (void)setWarmView:(UIView *)warmView{
    
    objc_setAssociatedObject(self, @"warmView", warmView, OBJC_ASSOCIATION_RETAIN);
}

- (UIView *)warmView{
    return objc_getAssociatedObject(self, @"warmView");
}

#pragma clang diagnostic pop

@end
